<template>
  <div class="mine-upload">
    <div class="title">我上传的课程</div>
    <div class="course-wrapper">
      <div class="content" v-if="(course.length === 0)">抱歉，您当前还没有上传任何课程</div>
      <swiper ref="mySwiper" :options="swiperOptions" class="courseList" v-else>
        <swiper-slide class="item" v-for="(item, index) in course" :key="index">
          <ke-intro :courseName="item.coursename" :price="item.price" :buyNumber="item.buyNum" />
          <div class="btn">
            <el-button type="primary" size="mini" @click="modifyCourse(index)" class="modify">修改</el-button>
            <el-button type="danger" size="mini" class="del" @click="delCourse(index)">删除</el-button>
          </div>
        </swiper-slide>
      </swiper>
    </div>
    <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
      <el-form :model="ruleForm">
        <el-form-item label="课程名称" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.courseName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="课程描述" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.description" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="课程价格" :label-width="formLabelWidth" prop="price">
          <el-input v-model="ruleForm.price"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="sendModify">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'mineUpload',
  inject: ['reload'],
  components: {
    keIntro: () => import('../ke-intro')
  },
  data() {
    return {
      swiperOptions: {
        direction: 'horizontal',
        spaceBetween: 10,
        width: 240,
        preventClicks: false
      },
      course: [],
      dialogFormVisible: false,
      ruleForm: {
        description: '',
        price: 0,
        courseName: ''
      },
      rules: {
        courseName: [
          { required: true, message: '请输入课程名称', trigger: 'blur' },
          {
            min: 3,
            max: 100,
            message: '长度在 3 到 100 个字符',
            trigger: 'blur'
          }
        ],
        description: [
          { required: true, message: '请输入课程描述', trigger: 'blur' },
          {
            min: 3,
            max: 500,
            message: '长度在 3 到 500 个字符',
            trigger: 'blur'
          }
        ],
        price: [{ required: true, message: '价格不能为空', trigger: 'blur' }]
      },
      formLabelWidth: '120px',
      courseIndex: 0
    }
  },
  mounted() {
    this.getMineUploadCourse()
  },
  methods: {
    getMineUploadCourse() {
      this.$axios
        .get('/api/teacher/teachCourse', {
          params: {
            teacherId: localStorage.getItem('ke_id')
          }
        })
        .then(res => {
          this.course = res.data.data
        })
    },
    modifyCourse(index) {
      this.dialogFormVisible = true
      this.ruleForm.courseName = this.course[index].coursename
      this.ruleForm.description = this.course[index].description
      this.ruleForm.price = this.course[index].price
      this.courseIndex = index
    },
    sendModify() {
      this.$axios
        .post('/api/teacher/updateCourse', {
          id: this.course[this.courseIndex]._id,
          courseName: this.ruleForm.courseName,
          description: this.ruleForm.description,
          price: this.ruleForm.price,
          teacherId: localStorage.getItem('ke_id')
        })
        .then(res => {
          this.dialogFormVisible = false
          this.$message({
            type: 'success',
            message: '修改成功'
          })
          this.reload()
        })
    },
    delCourse(index) {
      this.$axios
        .post('/api/teacher/delCourse', {
          teacherId: localStorage.getItem('ke_id'),
          courseId: this.course[index]._id
        })
        .then(res => {
          this.$message({
            type: 'success',
            message: '删除成功'
          })
          this.reload()
        })
    }
  }
}
</script>

<style lang="stylus" scoped>
.mine-upload {
  .title {
    font-size: 22px;
    margin-bottom: 20px;
    line-height: 1.3;
  }

  .course-wrapper {
    .content {
      text-align: center;
      margin-top: 200px;
      font-size: 14px;
      color: #999999;
    }

    .courseList {
      position: relative;
      height: 250px;
      overflow: hidden;
      padding-left: 0;
      overflow: hidden;
      border-top: 8px solid #eeeeee;
      padding-top: 8px;
      box-sizing: border-box;

      .item {
        list-style: none;
        float: left;
        width: 240px !important;
        height: 234px;
        box-sizing: border-box;
        cursor: pointer;
        border: 1px solid transparent;

        &:hover {
          border: 1px solid #dddddd;
        }

        .btn {
          position: absolute;
          bottom: 0;
          text-align: center;
          height: 28px;
          width: 100%;

          .modify {
            position: absolute;
            left: 20px;
          }

          .del {
            position: absolute;
            right: 40px;
          }
        }
      }
    }
  }
}
</style>
